<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\model\Order as OrderModel;
use app\common\controller\Backend;
use app\common\model\Room as RoomModel;
use app\common\model\User as UserModel;
use think\Request;

class CheckOut extends Backend
{
    //退房订单列表
    public function index(Request $request)
    {
        $roomList = RoomModel::select()->toArray();
        $userList = UserModel::select()->toArray();
        $roomIdMap = array_column($roomList, 'name', 'id');
        $userIdMap = array_column($userList, 'username', 'id');

        $orderList = OrderModel::where('status', 2)->select()->toArray();
        foreach ($orderList as &$order) {
            $order['room_name'] = $roomIdMap[$order['room_id']] ?? '';
            $order['username'] = $userIdMap[$order['user_id']] ?? '';
        }

        if (!empty($orderNo = $request->get('order_no'))) {
            $orderList = array_filter($orderList, function ($item) use ($orderNo) {
                return str_contains($item['order_no'], $orderNo);
            });
        }

        return $this->fetch('checkout/index', ['orderList' => $orderList]);
    }

    //退房订单保存
    public function cancel($id)
    {
        OrderModel::where('id', $id)->update(['status' => 3]);
        $this->redirect('index');
    }
}
